package ch.dreamweb.readpractice.client.lesetraining.meta.view;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.resources.client.CssResource;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.uibinder.client.UiTemplate;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

//import com.google.gwt.user.client.ui.KeyboardListenerAdapter;

public class LoginViewImpl extends Composite implements LoginView {

	private static LoginViewUiBinder uiBinder = GWT
			.create(LoginViewUiBinder.class);

	@UiTemplate("LoginView.ui.xml")
	interface LoginViewUiBinder extends UiBinder<Widget, LoginViewImpl> {
	}

	protected interface CssStyle extends CssResource {
		String big();
	}

	@UiField
	CssStyle style;

	@UiField
	Button buttonLogin;
	@UiField
	TextBox textBoxUsername;
	@UiField
	PasswordTextBox textBoxPassword;
	@UiField
	Label labelInfoLogin;
	@UiField
	Label labelPassword;
	@UiField
	Label labelUsername;

	private Presenter presenter;

	public LoginViewImpl() {
		initWidget(uiBinder.createAndBindUi(this));

		// Login
		textBoxUsername.setText("");
		textBoxUsername.setMaxLength(20);
		textBoxUsername.setVisibleLength(20);
		textBoxUsername.setReadOnly(false);
		textBoxUsername.setTabIndex(1);
		textBoxUsername.setFocus(true);
		// textBoxUsername.setFocus(true);
		textBoxPassword.setText("");
		textBoxPassword.setMaxLength(20);
		textBoxPassword.setVisibleLength(20);
		textBoxPassword.setReadOnly(false);
		textBoxPassword.setTabIndex(2);
		buttonLogin.setText("Login");
		buttonLogin.setTabIndex(3);
		buttonLogin.setFocus(true);
		labelInfoLogin.setText("");
		labelUsername.setText("Benutzername");
		labelPassword.setText("Passwort");

	}

	@Override
	public void setPresenter(Presenter presenter) {
		this.presenter = presenter;
	}

	@Override
	public void addClickListener() {
		textBoxUsername.addKeyPressHandler(new KeyPressHandler() {

			@Override
			public void onKeyPress(KeyPressEvent event) {
				char keyCode = event.getCharCode();
				if (keyCode == KeyCodes.KEY_PAGEDOWN) {
					presenter.onLoginButtonClicked("q", "");
				}
				if (keyCode == KeyCodes.KEY_ENTER) {
					textBoxPassword.setFocus(true);
					if (textBoxUsername.getText().equals("q")) {
						presenter.onLoginButtonClicked("q", "");
					}
				}
			}
		});
		textBoxPassword.addKeyPressHandler(new KeyPressHandler() {

			@Override
			public void onKeyPress(KeyPressEvent event) {
				char keyCode = event.getCharCode();
				if (keyCode == KeyCodes.KEY_ENTER) {
					presenter.onLoginButtonClicked(textBoxUsername.getText(),
							textBoxPassword.getText());

				}
			}
		});

	}

	@UiHandler("buttonLogin")
	void onButtonLoginClick(ClickEvent e) {
		if (presenter != null) {
			presenter.onLoginButtonClicked(textBoxUsername.getText(),
					textBoxPassword.getText());
		}

	}

	@Override
	public Widget asWidget() {
		return this;
	}

	@Override
	public void setInfoLogin(String text) {
		labelInfoLogin.setText(text);
		textBoxPassword.setText("");
	}

	@Override
	public void deleteEntries() {
		textBoxUsername.setText("");
		textBoxPassword.setText("");
	}

	@Override
	public void setLoginVisible(boolean visible) {
		labelInfoLogin.setVisible(visible);
		buttonLogin.setVisible(visible);
		textBoxUsername.setVisible(visible);
		textBoxPassword.setVisible(visible);
		labelUsername.setVisible(visible);
		labelPassword.setVisible(visible);
	}

	@Override
	public void setButtonVisible(boolean visible) {
		buttonLogin.setVisible(visible);
	}

	@Override
	public void setFocusOnUsername() {
		textBoxUsername.setFocus(true);
	}

}
